<?php
require_once 'common.php';

/**
 * 应用服务器配置备份模型
 * @author wangjiajun
 *
 */
class OaAppServerConfigLogModel extends OpModel
{
	
	// 应用服务器配置操作类型
    const SERVER_CONFIG_OP_ADD_AREA = 'area.add';                                            // 增加区
    const SERVER_CONFIG_OP_UPDATE_AREA = 'area.update';                                      // 修改区
    const SERVER_CONFIG_OP_DELETE_AREA = 'area.delete';                                      // 删除区
    const SERVER_CONFIG_OP_ADD_GROUP = 'group.add';                                          // 增加组
    const SERVER_CONFIG_OP_UPDATE_GROUP = 'group.update';                                    // 修改组
    const SERVER_CONFIG_OP_DELETE_GROUP = 'group.delete';                                     // 删除组
    const SERVER_CONFIG_OP_ADD_SERVER = 'server.add';                                        // 增加服务器
    const SERVER_CONFIG_OP_UPDATE_SERVER = 'server.update';                                  // 修改服务器
    const SERVER_CONFIG_OP_DELETE_SERVER = 'server.delete';                                  // 删除服务器
    const SERVER_CONFIG_OP_ADD_LOGIN_SERVER = 'login_server.add';                            // 增加登录服务器
    const SERVER_CONFIG_OP_UPDATE_LOGIN_SERVER = 'login_server.update';                      // 修改登录服务器
    const SERVER_CONFIG_OP_DELETE_LOGIN_SERVER = 'login_server.delete';                      // 删除登录服务器
    const SERVER_CONFIG_OP_ADD_LOGIN_SERVER_VALID_TO = 'login_server_valid.add';            // 增加登录服务器生效范围
    const SERVER_CONFIG_OP_UPDATE_LOGIN_SERVER_VALID_TO = 'login_server_valid.update';      // 修改登录服务器生效范围
    const SERVER_CONFIG_OP_DELETE_LOGIN_SERVER_VALID_TO = 'login_server_valid.delete';      // 删除登录服务器生效范围
    const SERVER_CONFIG_OP_ADD_PAY_SERVER = 'pay_server.add';                                // 增加充值服务器
    const SERVER_CONFIG_OP_UPDATE_PAY_SERVER = 'pay_server.update';                          // 修改充值服务器
    const SERVER_CONFIG_OP_DELETE_PAY_SERVER = 'pay_server.delete';                          // 删除充值服务器
    const SERVER_CONFIG_OP_ADD_PAY_SERVER_VALID_TO = 'pay_server_valid.add';                // 增加充值服务器生效范围
    const SERVER_CONFIG_OP_UPDATE_PAY_SERVER_VALID_TO = 'pay_server_valid.update';          // 修改充值服务器生效范围
    const SERVER_CONFIG_OP_DELETE_PAY_SERVER_VALID_TO = 'pay_server_valid.delete';          // 删除充值服务器生效范围
	
    private $_appSvrCfgLogTable;
    
    public static function getConstantValueByName($name, $default = 0)
    {
        return self::_getConstByName($name, '', $default, 'OaAppServerConfigBackupModel');
    }
    
    public function __construct($logger = null)
    {
        if (is_null($logger)) {
            $logger = getOaModelLogger();
        }
        parent::__construct($logger);
        $dbConfig = Oa_Util_Config::getSingleDbConfig(OA_WEB_DB_CLUSTER_ID);
        $db = OpDbModel::getInstance($dbConfig, $logger);
        $this->_appSvrCfgLogTable = new OpTableModel($db, 'app_server_config_log', $logger);
    }
    
    /**
     * 增加一条备份记录
     * @param int $appId
     * @param string $op
     * @param mixed $oldData
     * @param mixed $newData
     * @param int $env
     * @return int|boolean
     */
    public function addBackup($appId, $op, $oldData, $newData, $env)
    {
    	return $this->_appSvrCfgLogTable->insert(array(
    		'app_id' => $appId,
    		'environment' => $env,
    		'op' => $op,
    		'old_data' => json_encode($oldData),
    		'new_data' => json_encode($newData),
    		'create_time' => date('Y-m-d H:i:s')
    	));
    }
    
    /**
     * 查询备份记录
     * @param int $id
     * @return array|null|boolean
     */
    public function getBackup($id)
    {
    	$row = $this->_appSvrCfgLogTable->get(array(
    		'id' => $id
    	));
    	if (is_array($row)) {
    		$row['old_data'] = json_decode($row['old_data'], true);
    		$row['new_data'] = json_decode($row['new_data'], true);
    	}
    	return $row;
    }
    
    /**
     * 根据指定条件查询备份列表
     * @param array $where
     * @param int $offset
     * @param int $count
     * @param string $sort
     * @param array $otherWhere
     * @return array|boolean
     */
    public function getBackupList($where = null, $offset = 0, $count = null, 
        $sort = null, $otherWhere = null)
    {
        $sql = "SELECT * FROM app_server_config_log" . getSqlCond($where, $offset, 
        	$count, $sort, $otherWhere);
        $rows = $this->_appSvrCfgLogTable->query($sql);
        if ($rows === false) {
            $this->_logger->log("query failed");
            return false;
        }
        foreach ($rows as &$row) {
    		$row['old_data'] = json_decode($row['old_data'], true);
    		$row['new_data'] = json_decode($row['new_data'], true);
        }
        return $rows;
    }
    
    /**
     * 根据指定条件查询备份总数
     * @param array $where
     * @param array $otherWhere
     * @return int|boolean
     */
    public function getBackupListCount($where = null, $otherWhere = null)
    {
        $sql = "SELECT COUNT(*) FROM app_server_config_log" . getSqlCond($where, 
        	0, 0, null, $otherWhere);
        $row = $this->_appSvrCfgLogTable->query($sql, PDO::FETCH_NUM);
        if ($row === false) {
            $this->_logger->log("query failed");
            return false;
        }
        return (int) $row[0][0];
    }
    
    /**
     * 查询某个应用的服务器配置备份列表
     * @param int $appId
     * @param int $offset
     * @param int $count
     * @param int $env
     * @return array|boolean
     */
    public function getAppBackup($appId, $offset, $count, $env)
    {
    	return $this->getBackupList(array(
    		'app_id' => $appId,
    		'environment' => $env
    	), $offset, $count, 'create_time DESC');
    }
    
    /**
     * 查询某个应用的服务器配置备份列表总数
     * @param int $appId
     * @param int $env
     * @return int|boolean
     */
    public function getAppBackupCount($appId, $env)
    {
    	return $this->getBackupListCount(array(
    		'app_id' => $appId,
    		'environment' => $env
    	));
    }
    
    /**
     * 修改备份记录
     * @param int $id
     * @param array $row
     * @return boolean
     */
    public function modifyBackup($id, $row)
    {
    	return $this->_appSvrCfgLogTable->update(array(
    		'id' => $id
    	), $row);
    }
    
    /**
     * 删除备份记录
     * @param int $id
     * @return boolean
     */
    public function deleteBackup($id)
    {
    	return $this->_appSvrCfgLogTable->delete(array(
    		'id' => $id
    	));
    }
}